<?php

/**
 * alunos actions.
 *
 * @package    GC
 * @subpackage alunos
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class alunosActions extends sfActions {

    public function executeEditarNotas($request) {

//        $this->forward404Unless($this->gc_disciplina = Doctrine_Core::getTable('GcDisciplina')->find(array($request->getParameter('d_id'))), sprintf('Não Existe Disciplina (%s).', $request->getParameter('d_id')));
//        $this->forward404Unless($this->gc_aluno = Doctrine_Core::getTable('GcAluno')->find(array($request->getParameter('a_id'))), sprintf('Não Existe Aluno (%s).', $request->getParameter('a_id')));
//        $this->forward404Unless($gc_alunoDisciplinas = Doctrine_Core::getTable('GcAlunoDisciplina')->findBy('aluno_id', array($request->getParameter('a_id'))), sprintf('Não Existe relação (%s).', $request->getParameter('a_id')));
//        foreach ($gc_alunoDisciplinas as $gc_alunoDisciplina) {
//            if ($this->gc_disciplina->getNome() == $gc_alunoDisciplina->getGcDisciplina()->getNome()) {
//                $this->form = new GcAlunoDisciplinaForm($gc_alunoDisciplina);
//            }
//        }
//        die (var_dump($this->getRoute()->getObject()));
        $this->alunoDisciplina = $this->getRoute()->getObject();
        $this->aluno = $this->getRoute()->getObject()->getGcAluno();
        $this->disciplina = $this->getRoute()->getObject()->getGcDisciplina();
        $this->form = new GcAlunoDisciplinaForm($this->alunoDisciplina);
    }

    public function executeSalvarNotas(sfWebRequest $request) {

//        die('xpto');
//        $this->forward404Unless($this->gc_disciplina = Doctrine_Core::getTable('GcDisciplina')->find(array($request->getParameter('d_id'))), sprintf('Não Existe Disciplina (%s).', $request->getParameter('d_id')));
//        $this->forward404Unless($this->gc_aluno = Doctrine_Core::getTable('GcAluno')->find(array($request->getParameter('a_id'))), sprintf('Não Existe Aluno (%s).', $request->getParameter('a_id')));
//        $this->forward404Unless($gc_alunoDisciplinas = Doctrine_Core::getTable('GcAlunoDisciplina')->findBy('aluno_id', array($request->getParameter('a_id'))), sprintf('Não Existe relação (%s).', $request->getParameter('a_id')));
//
//        foreach ($gc_alunoDisciplinas as $gc_alunoDisciplina) {
//            if ($this->gc_disciplina->getNome() == $gc_alunoDisciplina->getGcDisciplina()->getNome()) {
                $this->form = new GcAlunoDisciplinaForm($this->getRoute()->getObject());

                $this->processForm($request, $this->form);

                $this->setTemplate('editarNotas');
            }
//        }
//    }

    public function executeIndex(sfWebRequest $request) {
        $this->gc_alunos = Doctrine_Core::getTable('GcAluno')
                ->createQuery('a')
                ->execute();
        $this->gc_disciplinas = Doctrine_Core::getTable('GcDisciplina')
                ->createQuery('d')
                ->execute();
    }

    public function executeNew(sfWebRequest $request) {
        $this->gc_aluno = new GcAluno();
        $this->form = new GcAlunoForm($this->gc_aluno);
    }

    public function executeCreate(sfWebRequest $request) {
        $this->form = new GcAlunoForm();

        $this->processForm($request, $this->form);
        $this->gc_aluno = $this->getRoute()->getObject();
        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        $this->gc_aluno = $this->getRoute()->getObject();
        $this->form = new GcAlunoForm($this->gc_aluno);
    }

    public function executeUpdate(sfWebRequest $request) {

        $this->form = new GcAlunoForm($this->getRoute()->getObject());

        $this->processForm($request, $this->form);

        $this->setTemplate('edit');

        $this->redirect('@alunos');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $gc_aluno = $this->getRoute()->getObject();
        $gc_aluno->delete();

        $this->redirect('@alunos');
    }

    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName())
        );
        if ($form->isValid()) {
            $job = $form->save();
            $this->redirect('@alunos');
        }
    }

}
